package example.com.terry.servlet;

import example.com.terry.bean.NoticDto;
import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 公告查看接口 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/queryNoticServlet")
public class QueryNoticServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (CookieUtils.isSign(req)) {

            String id1 = req.getParameter("id");
            if (id1 != null) {
                // 如果id不为空执行修改前的查询
                String sql = "select * from tb_notice where id='" + id1 + "'";
                ResultSet resultSet = JDBCUtils.initQuery(sql);
                try {
                    if (resultSet.next()) {
                        String id = resultSet.getString("id");
                        //公告内容

                        String noticeText = resultSet.getString("notice_text");
                        //创建人
                        String noticeMan = resultSet.getString("notice_man");
                        // 公告时间
                        String createTime = resultSet.getString("create_time");
                        //公告类型
                        String noticType = resultSet.getString("notice_type");
                        NoticDto noticDto = new NoticDto();
                        noticDto.setId(id);
                        noticDto.setCreateTime(createTime);
                        noticDto.setNoticeMan(noticeMan);
                        noticDto.setNoticeText(noticeText);
                        noticDto.setNoticType(noticType);
                        resp.getWriter().println(noticDto);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } else {
                ArrayList<NoticDto> noticDtos = new ArrayList<>();
                String sql = "select * from tb_notice  order by create_time desc";
                ResultSet resultSet = JDBCUtils.initQuery(sql);
                try {
                    while (resultSet.next()) {
                        String id = resultSet.getString("id");
                        //公告内容

                        String noticeText = resultSet.getString("notice_text");
                        //创建人
                        String noticeMan = resultSet.getString("notice_man");
                        // 公告时间
                        String createTime = resultSet.getString("create_time");
                        //公告类型
                        String noticType = resultSet.getString("notice_type");
                        NoticDto noticDto = new NoticDto();
                        noticDto.setId(id);
                        noticDto.setCreateTime(createTime);
                        noticDto.setNoticeMan(noticeMan);
                        noticDto.setNoticeText(noticeText);
                        noticDto.setNoticType("1".equals(noticType) ? "学校公告" : "班级公告");

                        noticDtos.add(noticDto);
                    }
                } catch (Exception e) {

                }

           /* req.setAttribute("message", noticDtos); // This will be available as ${message}
            req.getRequestDispatcher("/index2.jsp").forward(req, resp);

*/


                resp.getWriter().println(noticDtos);
            }
            // 返回当前页面请求的数据 noticDtos
        } else {
            resp.getWriter().println("当前非登陆状态请重新登陆");
            // req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }

    }
}
